package com.rchou.sts.service.jpa;

import java.util.Date;
import java.util.List;

import org.springframework.context.support.GenericXmlApplicationContext;

import com.rchou.sts.domain.Level;
import com.rchou.sts.domain.Student;
import com.rchou.sts.domain.StudentSummary;
import com.rchou.sts.service.StudentService;

public class JpaSample {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:root-context.xml");
		ctx.refresh();

		StudentService studentService = ctx.getBean(
				"studentService", StudentService.class);
		System.out.println("Get All:");
		List<Student> students = studentService.findAll();
		listStudents(students);
		
		System.out.println("Get All with Detail:");
		students = studentService.finAllWithDetail();
		listStudentsWithDetail(students);
		
		System.out.println("Get One by Id:");
		Student student = studentService.findById(new Long(1));
		System.out.println("");
		System.out.println("Student with details:");
		System.out.println(student);
		
		if (student.getLevels() != null) 
		{
			for (Level level: student.getLevels()) 
			{
				System.out.println(level);
			}
		}
		
		System.out.println("Get All with Summary");
		List<StudentSummary> studentSummaries = studentService.findAllSummary();
		listStudentSummary(studentSummaries);
		
		// Add new student
		System.out.println("Add New Student");
		Student student1 = new Student();
		student1.setFirstName("FirstName3");
		student1.setLastName("LastName3");
		student1.setAddress("Earth3");
		student1.setPhone("333-333-3333");
		student1.setEmail("mail3@mail.com");
		student1.setParentFirstName("ParentFirstName3");
		student1.setParentLastName("ParentLastName3");		
		Level lvlHistory = new Level();
		lvlHistory.setSubmitDate(new Date());
		lvlHistory.setLevel(1);
		student1.addLvlHistory(lvlHistory);
		studentService.save(student1);
		students = studentService.finAllWithDetail();
		listStudentsWithDetail(students);
		
		// Update student by Id
		System.out.println("Update One by Id:");
		student.setFirstName("King Kong");
		List<Level> lvlHistories = student.getLevels();
		lvlHistories.remove(0);
		studentService.save(student);
		students = studentService.finAllWithDetail();
		listStudentsWithDetail(students);
		
		// Delete student by Id
		System.out.println("Delete One by Id:");
		student = studentService.findById(new Long(1));
		studentService.delete(student);
		students = studentService.finAllWithDetail();
		listStudentsWithDetail(students);
		
		// Native query
		System.out.println("Native Query:");
		students = studentService.findAllByNativeQuery();
		students = studentService.finAllWithDetail();
		listStudentsWithDetail(students);
	}

	private static void listStudents(List<Student> students) 
	{
		System.out.println("");
		System.out.println("Listing students without details:" + students.size());
		for (Student student: students) 
		{
			System.out.println(student);
			System.out.println();
		}
	}
	
	private static void listStudentsWithDetail(List<Student> students) 
	{
		System.out.println("");
		System.out.println("Listing students with details:" + students.size());
		for (Student student: students) 
		{
			System.out.println(student);
			if (student.getLevels() != null) 
			{
				for (Level level: student.getLevels()) 
				{
					System.out.println(level);
				}
			}
			/*if (contact.getHobbies() != null) 
			{
				for (Hobby hobby: contact.getHobbies()) 
				{
					System.out.println(hobby);
				}
			}*/
			System.out.println();
		}
	}
	
	private static void listStudentSummary(List<StudentSummary> students) 
	{
		System.out.println("");
		System.out.println("Listing student summary:" + students.size());
		for (StudentSummary student: students) 
		{
			System.out.println(student);
			System.out.println();
		}
	}
}
